package com.yuyou.hxyy.gateway.fiter;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuyou.hxyy.service.api.bean.LoginRedisObj;
import com.yuyou.hxyy.service.api.bean.ReturnCode;
import com.yuyou.hxyy.service.api.bean.ReturnResult;
import com.yuyou.hxyy.service.api.cons.CommCons;
import com.yuyou.hxyy.service.api.util.SpringUtil;

public class AccessFilter implements Filter{

	
	@Value("${enable.swagger}")
	private  boolean swagger;
	
	@Value("${enable.on}")
	private  boolean on;
	
	private StringRedisTemplate stringRedisTemplate;
	
	private static  List<String> exclusiveList=new ArrayList<String>();
				
	static {		
		exclusiveList.add("login");
		exclusiveList.add("api-docs");
		exclusiveList.add("Captcha");
		exclusiveList.add("swagger");
		exclusiveList.add("health");
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {	
	}
	
	
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
		if(on){
			chain.doFilter(httpServletRequest, response);
			return;
		}
		
		String uri=httpServletRequest.getRequestURI();
		//登录绕过拦截器
		if(!swagger && uri.contains("swagger")){
			chain.doFilter(httpServletRequest, response);
			return;
		}
		for(String exUri:exclusiveList){
			if(uri.contains(exUri)){
			chain.doFilter(httpServletRequest, response);
	        return;
			}
		}
			
		String accessToken=httpServletRequest.getHeader("accessToken");	
		if(StringUtils.isEmpty(accessToken)){
			out(httpServletResponse);
			return;
		}
		
		stringRedisTemplate=(StringRedisTemplate)SpringUtil.getBean("stringRedisTemplate");
		
		String jsonObj=stringRedisTemplate.opsForValue().get(accessToken);
		if(StringUtils.isEmpty(jsonObj)){
			out(httpServletResponse);
			return;
		}
		LoginRedisObj obj=JSONObject.parseObject(jsonObj, new TypeReference<LoginRedisObj>() {});
		//延长时间
		stringRedisTemplate.expire(accessToken, CommCons.REDIS_EXPIRE_TIME,CommCons.REDIS_EXPIRE_TIME_UNIT);     
		stringRedisTemplate.expire(String.valueOf(obj.getUserId()), CommCons.REDIS_EXPIRE_TIME,CommCons.REDIS_EXPIRE_TIME_UNIT);
		chain.doFilter(httpServletRequest, response);
	}

		
	private void out(HttpServletResponse httpServletResponse) throws IOException {
		ReturnResult<String> resp=ReturnResult.returnRes(false,ReturnCode.code._3001.getMessage(),null);			
		httpServletResponse.setHeader("Content-type", "text/html;charset=UTF-8");
		httpServletResponse.setCharacterEncoding("UTF-8");
		String data = resp.toJsonString();
		PrintWriter pw = httpServletResponse.getWriter();
		pw.write(data);
	}
	
	
	@Override
	public void destroy() {	
	}
	


}
